package com.gjintel.framework.menu.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gjintel.framework.cache.InitCacheData;
import com.gjintel.framework.cache.InitCacheData.CacheType;
import com.gjintel.framework.menu.model.Model;
import com.gjintel.framework.role.model.Role;

@RequestMapping("/portal/top")
@Controller
public class MenuTopController {

	@SuppressWarnings("unchecked")
	@RequestMapping("/userModel")
	public ModelAndView userModel(HttpSession session) {
		ModelAndView mv = new ModelAndView("portal/top-menu");
		List<Model> oneModel = new ArrayList<Model>();
		Map<String, List<Model>> twoModel = new HashMap<String, List<Model>>();
		Object obj = session.getAttribute("loginUserRole");
		if (obj != null) {
			List<Role> roleList = (List<Role>) obj;
			Iterator<Role> roleIt = roleList.iterator();
			Role role = null;
			Map<String, Model> modelMap = new HashMap<String, Model>();
			List<Model> modelList = null;
			Iterator<Model> modelIt = null;
			Model model = null;
			while (roleIt.hasNext()) {
				role = roleIt.next();
				modelList = InitCacheData.getInstance().getCacheTreeData(CacheType.ROLE_MODEL).get(role.getId());
				modelIt = modelList.iterator();
				while (modelIt.hasNext()) {
					model = modelIt.next();
					modelMap.put(model.getId(), model);
				}
			}
			this.madeModel(modelMap, oneModel, twoModel);
			mv.addObject("oneModel", oneModel);
			mv.addObject("twoModel", twoModel);
		}
		return mv;
	}

	/**
	 * @方法名：madeOneModel
	 * @功能说明：构造一级菜单信息
	 * @author fengya
	 * @date 2014-10-14 下午05:44:46
	 * @param modelMap
	 * @return void
	 */
	@SuppressWarnings("unchecked")
	private void madeModel(Map<String, Model> modelMap, List<Model> oneModel, Map<String, List<Model>> twoModel) {
		List<Model> oneModels = InitCacheData.getInstance().getCacheTreeData(InitCacheData.CacheType.MENU).get(InitCacheData.ROOT);
		Model rootModel = null;
		if (oneModels != null && !oneModels.isEmpty()) {
			rootModel = oneModels.get(0);
			oneModels = InitCacheData.getInstance().getCacheTreeData(InitCacheData.CacheType.MENU).get(rootModel.getId());
			Iterator<Model> oneIt = oneModels.iterator();
			Model model = null;
			Model temp = null;
			List<Model> twoModels = null;
			Iterator<Model> twoIt = null;
			while (oneIt.hasNext()) {
				model = oneIt.next();
				if (modelMap.containsKey(model.getId())) {
					oneModel.add(model);
					twoModels = InitCacheData.getInstance().getCacheTreeData(InitCacheData.CacheType.MENU).get(model.getId());
					if (twoModels != null && !twoModels.isEmpty()) {
						twoIt = twoModels.iterator();
						while (twoIt.hasNext()) {
							temp = twoIt.next();
							if (modelMap.containsKey(temp.getId())) {
								if (twoModel.get(model.getId()) == null) {
									twoModel.put(model.getId(), new ArrayList<Model>());
								}
								twoModel.get(model.getId()).add(temp);
							}
						}
					}
				}
			}
		}
	}
}
